package com.tech.cloudapp.util;


import sun.misc.BASE64Decoder;

import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.spec.ECPoint;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Map;

public class KeyStoreFactory {

    private static KeyStoreFactory instance = null;
    private static final Object lock = new Object();
    private KeyStore keyStore = null;
    private SSLContext sslcontext;
    private Map<String, Object> tokenMap;
    private BigInteger merchantPrivatekey;
    private ECPoint openPublicKey;
    private X509Certificate openCert;
    private String cntrkey;
    private String syncKey;
    private String token;
    private String gmmerchantPrivatekey;
    private String gmopenPublicKey;
    private X509TrustManager xtm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    public Map<String, Object> getTokenMap() {
        return this.tokenMap;
    }

    public void setTokenMap(Map<String, Object> tokenMap) {
        this.tokenMap = tokenMap;
    }

    public static KeyStoreFactory getInstance() throws Exception {
        if (instance == null) {
            synchronized(lock) {
                if (instance == null) {
                    instance = new KeyStoreFactory();
                }
            }
        }

        return instance;
    }

    private KeyStoreFactory() throws Exception {
        this.createLZSSLContext();
    }

    private void createLZSSLContext() throws Exception {
        try {
            this.sslcontext = SSLContext.getInstance("TLS");
            X509TrustManager[] xcf = new X509TrustManager[]{this.xtm};
            this.sslcontext.init((KeyManager[])null, xcf, new SecureRandom());
        } catch (Exception var2) {

            throw new Exception("加载ssl异常");
        }
    }

    private static PrivateKey getPrivateKey(String privateKey) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec((new BASE64Decoder()).decodeBuffer(privateKey));
        PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec);
        return priKey;
    }

    public SSLContext getSslcontext() {
        return this.sslcontext;
    }

    public String getCntrkey() {
        return this.cntrkey;
    }

    public void setCntrkey(String cntrkey) {
        this.cntrkey = cntrkey;
    }

    public String getSyncKey() {
        return this.syncKey;
    }

    public void setSyncKey(String syncKey) {
        this.syncKey = syncKey;
    }

    public String getToken() {
        return this.token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public BigInteger getMerchantPrivatekey() {
        return this.merchantPrivatekey;
    }

    public void setMerchantPrivatekey(BigInteger merchantPrivatekey) {
        this.merchantPrivatekey = merchantPrivatekey;
    }

    public X509Certificate getOpenCert() {
        return this.openCert;
    }

    public void setOpenCert(X509Certificate openCert) {
        this.openCert = openCert;
    }

    public void setmerchantPrivatekey(BigInteger merchantPrivatekey) {
        this.merchantPrivatekey = merchantPrivatekey;
    }

    public ECPoint getOpenPublicKey() {
        return this.openPublicKey;
    }

    public void setOpenPublicKey(ECPoint openPublicKey) {
        this.openPublicKey = openPublicKey;
    }

    public String getGmmerchantPrivatekey() {
        return this.gmmerchantPrivatekey;
    }

    public void setGmmerchantPrivatekey(String gmmerchantPrivatekey) {
        this.gmmerchantPrivatekey = gmmerchantPrivatekey;
    }

    public String getGmopenPublicKey() {
        return this.gmopenPublicKey;
    }

    public void setGmopenPublicKey(String gmopenPublicKey) {
        this.gmopenPublicKey = gmopenPublicKey;
    }

    private static class TrustAnyTrustManager implements X509TrustManager {
        private TrustAnyTrustManager() {
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }

}
